<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use app\wechat\controller\User;
use app\admin\service\ApiService;
use think\Db;

class MeetingController extends HomeBaseController
{
	//预约会场
	public function index()
	{
        if (date("H") > 12 ) {
            $start_time = strtotime(date("Y-m-d 12:00:00"));
            $end_time = strtotime(date("Y-m-d 18:00:00"));
            $this->assign('timetype',2);
        }else{
            $start_time = strtotime(date("Y-m-d 09:00:00"));
            $end_time = strtotime(date("Y-m-d 12:00:00"));
            $this->assign('timetype',1);
        }
		

		$data = Db::name('meeting')
			->field('id,list_title,list_subtitle')
			->select()
			->toArray();

		$data_book = Db::name('meeting_book')
            ->where(['delete_time'=>0])
			->whereTime('start_time', 'between', [$start_time,$end_time])
            ->order('start_time desc')
			->select()
            ->toArray();

		for ($i=0,$count=count($data); $i < $count; $i++) { 
			for ($j=0,$countj = count($data_book); $j < $countj; $j++) { 
                if ($data[$i]['id'] == $data_book[$j]['meet_id']) {
                    $data[$i]['book'] = $data_book[$j];
                }
            }   
		}
        $this->assign('info',$data);//会议室信息
    	return $this->fetch();
    }
    //时间筛选接口
    public function indexCheck()
    {
        $data = $this->request->param();

        if ( (strtotime($data['sdate'])+60*60*24) < time()) {
            echo json_encode(array('msg'=>'当前时间不可选择','status'=>0));return;
        }

        if ($data['timetype'] == "上午") {
            if( date('Y-m-d') == $data['sdate'] ){
                if ( date("H") > 12  ) {
                    echo json_encode(array('msg'=>'时间不可选择','status'=>0));return;
                }
                
            }
            $start_time = strtotime(date($data['sdate']." 09:00:00"));
            $end_time = strtotime(date($data['sdate']." 12:00:00"));
        }elseif ($data['timetype'] == "下午") {
            $start_time = strtotime(date($data['sdate']." 14:00:00"));
            $end_time = strtotime(date($data['sdate']." 18:00:00"));
        }
        
        $data = Db::name('meeting')
            ->field('id,list_title,list_subtitle')
            ->select()
            ->toArray();

        $data_book = Db::name('meeting_book')
            ->where(['delete_time'=>0])
            ->whereTime('start_time', 'between', [$start_time,$end_time])
            ->order('start_time desc')
            ->select()
            ->toArray();
        $countj = count($data_book);
        $count=count($data);
        for ($i=0; $i < $count; $i++) { 
            $data[$i]['book'] = '';
            if ($countj == 0) {
                $data[$i]['book'] = "";
            }

            foreach ($data_book as $key => $value) {
                if ($data[$i]['id'] == $value['meet_id']) {
                    $data[$i]['book'] = $value;
                }
            }
        }
        $result = json_encode(array('msg'=>$data,'status'=>1));
        echo $result; return;
    }
    //预定会场详情页面
    public function add()
    {
        $userid = cmf_get_current_user_id();
        $user = Db::name('member')
        ->alias('a')
        ->join('cmf_wechat_department_user b','a.openid=b.user_id','left')
        ->join('cmf_wechat_department c','c.id=b.department_id','left')
        ->field('c.name as name,a.user_login,a.mobile,a.id')
        ->where('a.id',$userid)
        ->find();
        $this->assign('user',$user);

    	$id = $this->request->param('id', 0, 'intval');
    	$data = $this->request->param();

        //$this->assign('time',$data);
    	
    	$meet_info = Db::name('meeting')->find($id);
    	$meeting_article  = cmf_get_option('meeting_article');
        $this->assign('meeting_article',$meeting_article);
    	$this->assign('meet_info',$meet_info);
    	$meet_book_info = Db::name('meeting_book')
	    	->select();

    	return $this->fetch();
    }
    //筛选时间检测预定页面接口
    public function addCheck()
    {
        $data = $this->request->param();

        if ( (strtotime($data['sdate'])+60*60*24) < time()) {
            echo json_encode(array('msg'=>'当前时间不可选择','status'=>0));return;
        }

        if ($data['timetype'] == "上午") {
            if( date('Y-m-d') == $data['sdate'] ){
                if ( date("H") > 12  ) {
                    echo json_encode(array('msg'=>'时间不可选择','status'=>0));return;
                }
            }
            $start_time = strtotime(date($data['sdate']." 09:00:00"));
            $end_time = strtotime(date($data['sdate']." 12:00:00"));
        }elseif ($data['timetype'] == "下午") {
            $start_time = strtotime(date($data['sdate']." 14:00:00"));
            $end_time = strtotime(date($data['sdate']." 18:00:00"));
        }

        if (isset($data['id'])&&!empty($data['id'])) {
            $data_book = Db::name('meeting_book')
            ->where(['id'=>['neq',$data['id']],'meet_id'=>$data['meet_id'],'delete_time'=>0])
            ->whereTime('start_time', 'between', [$start_time,$end_time])
            ->order('start_time desc')
            ->select()
            ->toArray();
        }else{
            $data_book = Db::name('meeting_book')
            ->where(['meet_id'=>$data['meet_id'],'delete_time'=>0])
            ->whereTime('start_time', 'between', [$start_time,$end_time])
            ->order('start_time desc')
            ->select()
            ->toArray();
        }
        
        $countj = count($data_book);

        if ($countj == 0) {
            $data['book'] = "";
            echo json_encode(array('msg'=>'可预约','status'=>1));
            return;
        }else{
            echo json_encode(array('msg'=>'已被预约','status'=>0));
            return;
        }
    }

    //会议预约接口
    public function addPost()
    {
        if ($this->request->isPost()) {

            $data  =  $this->request->param();
            $data['user_id'] =  cmf_get_current_user_id();
            if (empty($data['theme'])) {
                echo  json_encode(array('msg'=>"会议主题不能为空",'status'=>0));exit;
            } 
            if ( !is_numeric($data['num']) || $data['num'] <= 0 ) {
                echo json_encode(array('msg'=>"会议人数必须为数字且大于0",'status'=>0));exit;
            }
            $data['start_time'] = strtotime(date($data['start']));
            $data['end_time'] = strtotime(date($data['end']));
            if ( $data['start_time'] < time() ) {
                echo json_encode(array('msg'=>"会议开始时间必须晚于当前时间！",'status'=>0));exit;
            }
            if ( $data['start_time'] >= $data['end_time']  ) {
                echo json_encode(array('msg'=>"会议结束时间必须晚于会议开始时间！",'status'=>0));exit;
            }
            $timewhere = ['gt',time()];
            $book_info = Db::name('meeting_book')->where(['meet_id'=>$data['meet_id'],'cancel'=>['neq',1],'start_time'=>$timewhere])->select();
            for ($i=0,$count = count($book_info); $i < $count; $i++) { 
                if ( ($data['start_time'] >= $book_info[$i]['start_time']) && ($data['start_time'] < $book_info[$i]['end_time']) ) {
                    echo json_encode(array('msg'=>"会议时间与其他会议时间有冲突！",'status'=>0));exit;
                }
                if (($data['start_time'] <= $book_info[$i]['start_time']) && ($data['end_time'] >= $book_info[$i]['end_time'])) {
                    echo json_encode(array('msg'=>"会议时间与其他会议时间有冲突！",'status'=>0));exit;
                }
            }
            unset($data['start']);unset($data['end']);
            $data['time'] = time();
            $res = Db::name('meeting_book')->insertGetId($data);
            if ($res) {
                $info = Db::name('member')
                ->alias('a')
                ->join('cmf_wechat_department_user b','a.openid=b.user_id','left')
                ->join('cmf_wechat_department c','c.id=b.department_id','left')
                ->field('c.name as name,a.openid,a.user_login')
                ->where('a.id',$data['user_id'])
                ->find();

                $admin = Db::name('wechat_tag_user')
                ->where('tagid',config('meet_admin'))
                ->column('userid');

                $meet_admin = implode("|",$admin);

                $book_data = Db::name('meeting_book')
                ->alias('a')
                ->join('meeting b','a.meet_id=b.id')
                ->field('a.*,b.list_title')
                ->where('a.id',$res)
                ->find();
                $user = new User();
                $descr = "<div class=\"normal\">温馨提示：您已成功预约".date('Y-m-d H:00',$book_data['start_time'])."至".date('H:00',$book_data['end_time']).$book_data['list_title']."，订单号：".$book_data['id']."，管理员收到订单后将第一时间为您审核，请耐心等待，谢谢。</div>";

                $url = url('portal/meeting/book','','html',true);

                

                $descr2 = "<div class=\"normal\">温馨提示：".$info['name'].$info['user_login']."预约的".date('Y-m-d H:00',$book_data['start_time'])."至".date('H:00',$book_data['end_time']).$book_data['list_title']."，订单号：".$book_data['id']."，请及时审核。</div>";
                try {
                    @$user->send($meet_admin,'会场预约',$descr2,$url);
                    @$user->send($info['openid'],'会场预约',$descr,$url);
                } catch (Exception $e) {
                    return true;
                }

                $result = json_encode(array('msg'=>"预定成功",'status'=>1,'url'=>"http://".$_SERVER['HTTP_HOST'].url('book')));
            }else{
                $result = json_encode(array('msg'=>"预定失败，请重新预定",'status'=>0));
            }
            
        }
        echo $result; return;
        
    }
    //编辑预定会议信息
    public function edit()
    {
    	$id = $this->request->param('id', 0, 'intval');
    	$meeting_book = Db::name('meeting_book')
            ->alias('a')
            ->join('cmf_meeting b','a.meet_id=b.id')
            ->field('a.*,b.title,b.is_layout as layout,b.is_type,b.intro')
            ->find($id);
        $user = Db::name('member')
        ->alias('a')
        ->join('cmf_wechat_department_user b','a.openid=b.user_id','left')
        ->join('cmf_wechat_department c','c.id=b.department_id','left')
        ->field('c.name as name,a.user_login,a.mobile,a.id')
        ->where('a.id',$meeting_book['user_id'])
        ->find();
        $this->assign('user',$user);

        $meeting_article  = cmf_get_option('meeting_article');
        $this->assign('meeting_article',$meeting_article);

	    $this->assign('info',$meeting_book);
    	return $this->fetch();
    }
    //我的预定页面
    public function book()
    {
        $img = Db::name('img')->find(3);
        $this->assign('img',$img);
        $userid = cmf_get_current_user_id();

        $msg_id = $this->request->param('msg_id', 0, 'intval');
        if (!empty($msg_id)) {
            $res = Db::name('new_msg_user')->where(['user_id'=>$userid,'msg_id'=>$msg_id])->find();
            if(!$res){
                Db::name('new_msg_user')->insert(['user_id'=>$userid,'msg_id'=>$msg_id]);
            }
        }
        $user = Db::name('member')
        ->alias('a')
        ->join('cmf_wechat_tag_user b','a.openid=b.userid')
        ->where('a.id',$userid)
        ->column('b.tagid');
        //print_r($user);
        if (in_array(config('meet_admin'), $user)) {
            $user_type = 3;
        }elseif(in_array(config('meet'), $user)){
            $user_type = 2;
        }else{
            $user_type = 1;
        }

        //$user_type = 3;
        $this->assign('user_type',$user_type);//1:普通用户 2:会务员查看 3: 会议管理员
       
    	$user_id = $this->request->param('user_id', 0, 'intval');
    	$meet = Db::name('meeting')->column('id,list_title');
        $this->assign('meet',$meet);
        $start_time =  strtotime(date("Y-m-d"));
        $end_time = strtotime("+1 month");

    	$where['a.user_id'] = $userid;
        $where['a.delete_time'] = ['eq',0];
        if ($user_type == 2) {
            unset($where['a.user_id']);
            $where['a.cancel'] = ['neq',1];
            $where['a.status'] = ['eq',1];
            $where['a.end_type'] = ['neq',1];
        }elseif ($user_type == 3) {
            unset($where['a.user_id']);
        }elseif ($user_type == 1) {
            $where['a.cancel'] = ['neq',1];
        }
    	$meet_book_info = Db::name('meeting_book')
            ->alias('a')
            ->join('cmf_member d','a.user_id=d.id','left')
            ->join('cmf_wechat_department_user b','d.openid=b.user_id','left')
            ->join('cmf_wechat_department c','c.id=b.department_id','left')
	    	->where($where)
            ->whereTime('a.start_time', 'between', [$start_time,$end_time])
            ->field('a.*,c.name')
            ->order('a.time desc')
	    	->select()->toArray();
	    $this->assign('book_info',$meet_book_info);

    	return $this->fetch();
    }
    //预定列表时间筛选接口
    public function bookCheck()
    {
        $data = $this->request->param();

       
        $start_time = strtotime(date($data['sdate']));
        $end_time = strtotime(date($data['edate']));
        
        $userid = cmf_get_current_user_id();
        $user = Db::name('member')
        ->alias('a')
        ->join('cmf_wechat_tag_user b','a.openid=b.userid')
        ->where('a.id',$userid)
        ->column('b.tagid');

        if (in_array(config('meet_admin'), $user)) {
            $user_type = 3;
        }elseif(in_array(config('meet'), $user)){
            $user_type = 2;
        }else{
            $user_type = 1;
        }
        //$this->assign('user_type',$user_type);//1:普通用户 2:会务员查看 3: 会议管理员
        //$user_type = 3;
        $user_id = $this->request->param('user_id', 0, 'intval');
        $meet = Db::name('meeting')->column('id,list_title');
        
        $part = Db::name('member')
            ->alias('a')
            ->join('cmf_wechat_department_user b','a.openid=b.user_id')
            ->join('cmf_wechat_department c','c.id=b.department_id')
            ->column('a.id,c.name');

        $where['user_id'] = $userid;
        $where['delete_time'] = ['eq',0];
        if ($user_type == 2) {
            unset($where['user_id']);
            $where['cancel'] = ['neq',1];
            $where['status'] = ['eq',1];
            $where['end_type'] = ['neq',1];
        }elseif ($user_type == 3) {
            unset($where['user_id']);
        }elseif ($user_type == 1) {
            $where['cancel'] = ['neq',1];
        }
        $meet_book_info = Db::name('meeting_book')
            ->where($where)
            ->whereTime('start_time', 'between', [$start_time,$end_time])
            ->order('time desc')
            ->select()->toArray();

        for ($i=0,$count = count($meet_book_info); $i < $count; $i++) { 
            $meet_book_info[$i]['change'] = 0;
            $meet_book_info[$i]['start_time'] = date('m-d H:00',$meet_book_info[$i]['start_time']);
            $meet_book_info[$i]['end_time'] =  date('H:00',$meet_book_info[$i]['end_time']);
            if(strtotime('-1 hour',$meet_book_info[$i]['start_time']) > time()){
                $meet_book_info[$i]['change'] = 1;
            }
            $meet_book_info[$i]['title'] = $meet[$meet_book_info[$i]['meet_id']]?$meet[$meet_book_info[$i]['meet_id']]:'';
            $meet_book_info[$i]['name'] = $part[$meet_book_info[$i]['user_id']]?$part[$meet_book_info[$i]['user_id']]:'';

        }
        $result = json_encode(array('msg'=>$meet_book_info,'usertype'=>$user_type));
        echo $result; return;
    }
    //个人预定详细页面
    public function book_detail()
    {

        $img = Db::name('img')->find(3);
        $this->assign('img',$img);

    	$id = $this->request->param('id', 0, 'intval');   

        $type = $this->request->param('type', 0, 'intval');

        //$user_type = 1;
        //$this->assign('user_type',$user_type);//1:普通用户 2:会务员查看 3: 会议管理员


        // $meet_book_info = Db::name('meeting_book')
        //     ->alias('a')
        //     ->join('cmf_member d','a.user_id=d.id')
        //     ->join('cmf_wechat_department_user b','d.openid=b.user_id')
        //     ->join('cmf_wechat_department c','c.id=b.department_id')
        //     ->where($where)
        //     ->whereTime('start_time', 'between', [$start_time,$end_time])
        //     ->field('a.*,c.name')
        //     ->select()->toArray();
        // $this->assign('book_info',$meet_book_info);
        $userid = cmf_get_current_user_id();
        $user = Db::name('member')
        ->alias('a')
        ->join('cmf_wechat_tag_user b','a.openid=b.userid','left')
        ->where('a.id',$userid)
        ->column('b.tagid');
        //print_r($user);
        if (in_array(config('meet_admin'), $user)) {
            $user_type = 3;
        }elseif(in_array(config('meet'), $user)){
            $user_type = 2;
        }else{
            $user_type = 1;
        }
        $this->assign('user_type',$user_type);
    	$meeting_book = Db::name('meeting_book')
        ->alias('a')
        ->join('cmf_meeting e','a.meet_id=e.id','left')
        ->join('cmf_member d','a.user_id=d.id','left')
        ->join('cmf_wechat_department_user b','d.openid=b.user_id','left')
        ->join('cmf_wechat_department c','c.id=b.department_id','left')
        ->field('a.*,e.list_title as title,e.is_layout as layout,d.user_login,d.mobile,c.name')
        ->where('a.id',$id)
        ->find();
    	$this->assign('info',$meeting_book);


    	
    	return $this->fetch();
    }
    public function book_detail_cancel()
    {

        $img = Db::name('img')->find(3);
        $this->assign('img',$img);
        $id = $this->request->param('id', 0, 'intval');   

        //$user_type = 1;
        //$this->assign('user_type',$user_type);//1:普通用户 2:会务员查看 3: 会议管理员


        $meeting_book = Db::name('meeting_book')
        ->alias('a')
        ->join('cmf_meeting e','a.meet_id=e.id','left')
        ->join('cmf_member d','a.user_id=d.id','left')
        ->join('cmf_wechat_department_user b','d.openid=b.user_id','left')
        ->join('cmf_wechat_department c','c.id=b.department_id','left')
        ->field('a.*,e.list_title as title,e.is_layout as layout,d.user_login,d.mobile,c.name')
        ->where('a.id',$id)
        ->find();
        $this->assign('info',$meeting_book);


        
        return $this->fetch();
    }
    public function book_detail_check()
    {

        $img = Db::name('img')->find(3);
        $this->assign('img',$img);
        $id = $this->request->param('id', 0, 'intval');   

        $user_type = 1;
        $this->assign('user_type',$user_type);//1:普通用户 2:会务员查看 3: 会议管理员


        // $meeting_book = Db::name('meeting_book')
        // ->alias('a')
        // ->join('cmf_meeting b','a.meet_id=b.id')
        // ->field('a.*,b.title,b.is_layout as layout')
        // ->find($id);
        $meeting_book = Db::name('meeting_book')
        ->alias('a')
        ->join('cmf_meeting e','a.meet_id=e.id','left')
        ->join('cmf_member d','a.user_id=d.id','left')
        ->join('cmf_wechat_department_user b','d.openid=b.user_id','left')
        ->join('cmf_wechat_department c','c.id=b.department_id','left')
        ->field('a.*,e.list_title as title,e.is_layout as layout,d.user_login,d.mobile,c.name')
        ->where('a.id',$id)
        ->find();
        $this->assign('info',$meeting_book);
        return $this->fetch();
    }

    //修改预定信息接口
    public function editPost()
    {
    	if ($this->request->isPost()) {
            $id  =  $this->request->param("id",0,'intval');
            $data_meet = Db::name('meeting_book')->find($id);
            if ($data_meet['status'] == 1 || $data_meet['end_type'] == 1) {
                echo  json_encode(array('msg'=>"预约已通过，不可修改",'status'=>0));return;
            }
            $data  =  $this->request->param();
            $data['user_id'] = cmf_get_current_user_id();
            if (empty($data['theme'])) {
                echo  json_encode(array('msg'=>"会议主题不能为空",'status'=>0));return;
            } 
            if ( !is_numeric($data['num']) || $data['num'] <= 0 ) {
                echo json_encode(array('msg'=>"会议人数必须为数字且大于0",'status'=>0));return;
            }
            $data['start_time'] = strtotime(date($data['start']));
            $data['end_time'] = strtotime(date($data['end']));
            if ( $data['start_time'] < time() ) {
                echo json_encode(array('msg'=>"会议开始时间必须晚于当前时间！",'status'=>0));return;
            }
            if ( $data['start_time'] >= $data['end_time']  ) {
                echo json_encode(array('msg'=>"会议结束时间必须晚于会议开始时间！",'status'=>0));return;
            }
            $timewhere = ['gt',time()];
            $book_info = Db::name('meeting_book')->where(['meet_id'=>$data['meet_id'],'cancel'=>['neq',1],'start_time'=>$timewhere])->select();
            for ($i=0,$count = count($book_info); $i < $count; $i++) { 
                if ( ($data['start_time'] >= $book_info[$i]['start_time']) && ($data['start_time'] < $book_info[$i]['end_time']) ) {
                    echo json_encode(array('msg'=>"会议时间与其他会议时间有冲突！",'status'=>0));return;
                }
                if (($data['start_time'] <= $book_info[$i]['start_time']) && ($data['end_time'] >= $book_info[$i]['end_time'])) {
                    echo json_encode(array('msg'=>"会议时间与其他会议时间有冲突！",'status'=>0));return;
                }
            }
            
            unset($data['start']);unset($data['end']);
            $data['time'] = time();
            $res = Db::name('meeting_book')->where(['id'=>$id])->update($data);
            if ($res) {
                $result = json_encode(array('msg'=>"修改成功",'status'=>1,'url'=>"http://".$_SERVER['HTTP_HOST'].url('book')));
            }else{
                $result = json_encode(array('msg'=>"修改失败，请重新操作",'status'=>0));
            }
        }
        echo $result; return;
    }

    //用户申请取消会议
    public function cancelSub()
    {
    	if ($this->request->isPost()) {
    		$id = $this->request->param('id', 0, 'intval');
    		$data  =  Db::name('meeting_book')->find($id);
            if ($data['status'] == 0) {
                $update['cancel'] = 2;
            }else if ($data['status'] == 1) {
                $update['delete_time'] = time();
                $update['cancel'] = 2;
            }
            
            $res = Db::name('meeting_book')->where(['id'=>$id])->update(['cancel'=>$cancel]);
            if ($res) {

                $admin = Db::name('wechat_tag_user')
                ->where('tagid',config('meet_admin'))
                ->column('userid');

                $meet_admin = implode("|",$admin);

                $book_info = Db::name('meeting_book')
                ->alias('a')
                ->join('meeting t','a.meet_id=t.id','left')
                ->join('member m','a.user_id=m.id','left')
                ->join('wechat_department_user b','m.openid=b.user_id','left')
                ->join('wechat_department c','c.id=b.department_id','left')
                ->field('a.id,c.name,m.user_login,a.theme,a.user_id,a.start_time,a.end_time,t.list_title,m.openid')
                ->where(['a.id'=>$id])->find();

                $user = new User();
                $url = url('portal/meeting/book','','html',true);

                $descr = "<div class=\"normal\">温馨提示：".$book_info['name'].$book_info['user_login']."使用的".date('Y-m-d H:00',$book_info['start_time'])."至".date('H:00',$book_info['end_time']).$book_info['list_title']."，订单号：".$book_info['id']." 费申请取消预约，请及时审核。</div>";
                 try {
                    @$user->send($meet_admin,'会场预约',$descr,$url);
                 } catch (Exception $e) {
                     return true;
                 }

            	$result = json_encode(array('msg'=>"申请成功",'status'=>1));
            }else{
            	$result = json_encode(array('msg'=>"申请失败，请重新操作",'status'=>0));
            }
            echo $result; return;
    	}
    }
    //取消会议审核
    public function cancelPost()
    {
    	if ($this->request->isPost()) {
    		$id = $this->request->param('id', 0, 'intval');
    		$data  =  $this->request->param();
    		if ($data['cancel'] == 1) {
                $data['delete_time'] = time();
            }
            $res = Db::name('meeting_book')->where(['id'=>$id])->update($data);
            if ($res) {

                if ($data['cancel'] == 1) {
                    $user = new User();
                    $book_info = Db::name('meeting_book')
                    ->alias('a')
                    ->join('meeting t','a.meet_id=t.id','left')
                    ->join('member m','a.user_id=m.id','left')
                    ->join('wechat_department_user b','m.openid=b.user_id','left')
                    ->join('wechat_department c','c.id=b.department_id','left')
                    ->field('a.id,c.name,m.user_login,a.theme,a.user_id,a.start_time,a.end_time,t.list_title,m.openid')
                    ->where(['a.id'=>$id])->find();

                    $url = url('portal/meeting/book','','html',true);
                    
                    $admin = Db::name('wechat_tag_user')
                    ->where('tagid',config('meet'))
                    ->column('userid');

                    $meet_admin = implode("|",$admin);

                    $descr = "<div class=\"normal\">温馨提示：".$book_info['name'].$book_info['user_login']."使用的".date('Y-m-d H:00',$book_info['start_time'])."至".date('H:00',$book_info['end_time']).$book_info['list_title']."申请取消预约已通过，订单号：".$book_info['end_time']."。</div>";

                    

                    $descr = "<div class=\"normal\">温馨提示：".$book_info['name'].$book_info['user_login']."使用的".date('Y-m-d H:00',$book_info['start_time'])."至".date('H:00',$book_info['end_time']).$book_info['list_title']."已取消，订单号：".$book_info['end_time']."。</div>";

                    try {
                        @$user->send($book_info['openid'],'会场预约',$descr,$url);
                        @$user->send($meet_admin,'会场预约',$descr2,$url);
                     } catch (Exception $e) {
                         return true;
                     }
                    
                }



            	$result = json_encode(array('msg'=>"操作成功",'status'=>1,'url'=>"http://".$_SERVER['HTTP_HOST'].url('book')));
            }else{
            	$result = json_encode(array('msg'=>"失败，请重新操作",'status'=>0));
            }
            echo $result; return;
    	}
    }
    //会议审核
    public function checkPost()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id', 0, 'intval');
            $data  =  $this->request->param();

            $res = Db::name('meeting_book')->where(['id'=>$id])->update($data);
            if ($res) {

                $book_info = Db::name('meeting_book')
                ->alias('a')
                ->join('meeting n','a.meet_id=n.id','left')
                ->join('member m','a.user_id=m.id','left')
                ->join('wechat_department_user b','m.openid=b.user_id','left')
                ->field('b.department_id,a.theme,a.user_id,n.list_title')
                ->where(['a.id'=>$id])->find();

                Db::name('new_msg')->insert(
                    [
                        'table_name'=>'meeting_book',
                        'time'=>time(),
                        'department_id'=> 0,
                        'content'=> $book_info['list_title']."预约",
                        'object_id'=>$id,
                        'user_id'=>$book_info['user_id'],
                        'type'=>4
                    ]);

                $info = Db::name('meeting_book')
                ->alias('a')
                ->join('cmf_member b','a.user_id=b.id')
                ->where(['a.id'=>$id])
                ->value('b.openid');
                
                $book_data = Db::name('meeting_book')
                ->alias('a')
                ->join('meeting b','a.meet_id=b.id')
                ->field('a.*,b.list_title')
                ->where('a.id',$id)
                ->find();

                $user = new User();
                if($param['status'] == 2){
                    $descr = "<div class=\"normal\">温馨提示：您预约的".date('Y-m-d H:00',$book_data['start_time'])."至".date('H:00',$book_data['end_time']).$book_data['list_title']."未通过审核，请重新预约其他时间段的会场，如有疑问请致电89088693、89088642。</div>";
                }elseif ($param['status'] == 1) {
                    $descr = "<div class=\"normal\">温馨提示：您预约的".date('Y-m-d H:00',$book_data['start_time'])."至".date('H:00',$book_data['end_time']).$book_data['list_title']."已通过审核，请按时到场使用会场，咨询请致电89088693、89088642。</div>";
                }


                $url = url('portal/meeting/book','','html',true);
                 try {
                    @$user->send($info,'会场预约',$descr,$url);
                 } catch (Exception $e) {
                    return true;
                 }
                

                $result = json_encode(array('msg'=>"操作成功",'status'=>1,'url'=>"http://".$_SERVER['HTTP_HOST'].url('book')));
            }else{
                $result = json_encode(array('msg'=>"失败，请重新操作",'status'=>0));
            }
            echo $result; return;
        }
    }

}
